package com.wingice.system.controller;


import com.wingice.common.web.ErrorCodeInfo;
import com.wingice.common.web.ResultBean;
import com.wingice.system.entity.SysResource;
import com.wingice.system.exception.SysBusinessCode;
import com.wingice.system.exception.SysBusinessException;
import com.wingice.system.service.SysOauthClientResourceService;
import com.wingice.system.validator.sys.oauthclientresource.SysOauthClientResourceValidator;
import com.wingice.system.vo.UserRoleResourceVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 客户端-用户关联 前端控制器
 * </p>
 *
 * @author 胡昊
 * @since 2022-01-10
 */
@RestController
@RequestMapping("/sysOauthClientResource")
@Tag(name = "客户端资源权限管理")
public class SysOauthClientResourceController {

    @Resource
    private SysOauthClientResourceService sysOauthClientResourceService;

    /**
     * @param userRoleResourceVo 客户端资源信息
     * @description 保存客户端的资源
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/27 9:23
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysOauthClientResource:saveOauthClientResource')")
    @PostMapping("/saveOauthClientResource")
    @Operation(summary = "保存客户端的资源")
    public ResultBean<Boolean> saveOauthClientResource(@RequestBody UserRoleResourceVo userRoleResourceVo) {
        //参数校验
        SysOauthClientResourceValidator.saveOauthClientResource(userRoleResourceVo);
        return ResultBean.restResult(sysOauthClientResourceService.saveOauthClientResource(userRoleResourceVo), ErrorCodeInfo.OK);
    }

    /**
     * @param oauthClientId 客户端id
     * @description 获取客户端下的资源
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/27 9:44
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysOauthClientResource:listOauthClientResource')")
    @GetMapping("/listOauthClientResource")
    @Operation(summary = "获取客户端下的资源")
    public ResultBean<List<SysResource>> listOauthClientResource(String oauthClientId) {
        //参数校验
        if (!StringUtils.hasText(oauthClientId)) {
            throw new SysBusinessException(SysBusinessCode.OAUTH_CLIENT_ID_EMPTY_ERROR);
        }
        return ResultBean.restResult(sysOauthClientResourceService.listOauthClientResource(oauthClientId), ErrorCodeInfo.OK);
    }
}

